package com.neudu.glsb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.glsb.commons.JSONReturn;
import com.neudu.glsb.pojo.SalesRecords;
import com.neudu.glsb.service.SalesRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class SalesRecordsController {

    @Autowired
    private SalesRecordsService salesRecordsService;
    @Autowired
    private JSONReturn jsonReturn;

    @PostMapping("get_all_sales_records")
    public String getAllSalesRecords(@RequestBody Map<String, Object> pageInfoMap) {
        try {
            Integer pageNum = (Integer) pageInfoMap.get("pageNum");
            Integer pageSize = (Integer) pageInfoMap.get("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<SalesRecords> list = salesRecordsService.list();
            PageInfo<SalesRecords> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("get_sales_records_by")
    public String getByCondition(@RequestBody Map<String, Object> conditionMap) {
        try {
            Integer pageNum = (Integer) conditionMap.get("pageNum");
            Integer pageSize = (Integer) conditionMap.get("pageSize");
            String salesDate = (String) conditionMap.get("salesDate");

            PageHelper.startPage(pageNum, pageSize);
            LambdaQueryWrapper<SalesRecords> queryWrapper = new LambdaQueryWrapper<>();
            if (!ObjectUtils.isEmpty(salesDate)) {
                queryWrapper.eq(SalesRecords::getSalesDate, salesDate);
            }
            List<SalesRecords> salesRecords = salesRecordsService.list(queryWrapper);
            PageInfo<SalesRecords> pageInfo = new PageInfo<>(salesRecords);
            return jsonReturn.returnSuccess(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}